import { _decorator, Component, Label, Node } from 'cc';
import { UIManager } from '../common/UIManager';
import HeroApi from '../api/HeroApi';
import { GroupItem } from './GroupItem';
import { Hero } from '../entity/Hero';
import { EventManager } from '../event/EventManager';
const { ccclass, property } = _decorator;

@ccclass('HeroGroup')
export class HeroGroup extends Component {
    @property(Label)
    heroNum:Label
    @property(Node)
    save:Node
    @property(Node)
    content:Node


    private count:number = 0

    start() {
        EventManager.instance.on(EventManager.EventType.HeroLocationChange, this.onHeroLocationChange, this)
       
        this.getList()
    }

    async getList(){
        await UIManager.instance.showLoading()
        let res = await HeroApi.Execute({op:1})
        UIManager.instance.closeLoading()
        
        let adChip = res.data.adChip
        let chip = res.data.chip
        let items = res.data.items
        
        let heroList = Hero.getListInHeroYard(items)
        console.log('heroList:', heroList)
        for(let hero of heroList){
            if(hero.loc){
                this.count ++
            }
        }
        this.heroNum.string = `(${this.count}/9)`

        let children = this.content.children
        for(let i = 0; i<children.length; i++){
            let ch = children[i]
            ch.getComponent(GroupItem).setData(heroList, i + 1)
        }
    }

   
    async onHeroLocationChange(){
        this.getList()
    }

    updateNum(){
        this.count = 0
        let children = this.content.children
        for(let i = 0; i<children.length; i++){
            let ch = children[i]
            let hero = ch.getComponent(GroupItem).hero
            if(hero && hero.loc){
                this.count ++
            }
        }
        this.heroNum.string = `(${this.count}/9)`
    }
    
}


